export default function sendMsg(msgType,Target,content) {
    const app = getApp();
    const ws = app.globalData.ws;
    const location = app.globalData.location;
    const user = app.globalData.user;
	const senderLocation=app.globalData.senderLocation
	var msgContent={
		text:"",
		voicePath:"",
		duration:0,
		sendGoodId:"",
		sendshopId:"",
		mediaPath:"",
		unsend:true,
		senderLocation:senderLocation
	}
    msgContent={...msgContent,...content}
    // 构建消息对象
    const msg = {
        sendUserId: user?.userId,
        userTel: user?.userTel,
		nicPic:user?.nicPic,
		nicName:user?.nicName,
	    content: msgContent,
        location: location,
        toTarget: Target,
        messageType: msgType,
		push_clientid:getApp().globalData.push_clientid,
        timestamp: Date.now(),
        receiveDistance: app.globalData.receiveDistance || 500,
		isRequest:false
    };

    try {
        // 检查 WebSocket 连接状态和必要数据是否存在
        if (ws && location && user && senderLocation) {
            ws.send({
				data:JSON.stringify(msg)
			});
            console.log('消息发送成功:', msg);
        } else {
            console.error('无法发送消息，WebSocket 连接未打开或必要数据缺失');
        }

        return msg;
    } catch (e) {
        // 捕获并记录错误信息
        console.error('发送消息时出现错误:', e.message);
        return null;
    }
}